<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Percy Doc Thrower</title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
        <script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.7.3/js/lib/beautify.js"></script>
        <script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.7.3/js/lib/beautify-css.js"></script>
        <script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.7.3/js/lib/beautify-html.js"></script>
        <script src="./lib/getQueryString.js"></script>
        <script src="./src/buildFunctionDocBlock.js"></script>
        <script src="./src/extractParameters.js"></script>
        <script src="./src/findFunction.js"></script>
        <script src="./src/findModule.js"></script>
        <script src="./src/getTypesList.js"></script>
        <script src="./src/parseFunctionSource.js"></script>
        <script src="./src/showParametersModal.js"></script>
    </head>
    <body>

        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="index.html">Percy - Function</a>
            <a class="navbar-brand" href="class.html" id="swapType">(swap to class)</a>
            <div class="navbar-nav">
                <a class="nav-item nav-link" href="index.html?filter=all&show=all">All</a>
                <a class="nav-item nav-link" href="index.html?filter=all&show=done">Done</a>
                <a class="nav-item nav-link" href="index.html?filter=all&show=todo">Remaining</a>
                <a class="nav-item nav-link" href="../out/">Output</a>
            </div>
        </nav>

        <div class="container-fluid">

            <div class="row">
                <div class="col">

                    <h2>Source</h2>

                    <form>

                        <div class="form-group">
                            <label for="class">Class</label>
                            <input type="text" id="class" class="form-control" value="Phaser." />
                        </div>

                        <div class="alert alert-danger" id="sourceAlert" role="alert" style="display: none;">Alert!</div>

                        <div class="form-group">
                            <textarea id="input" class="form-control" rows="33" style="font-family: Consolas" spellcheck="false"></textarea>
                        </div>
                        <div class="form-group">
                            <button type="button" id="parseButton" class="btn btn-primary">Parse</button>
                            <button type="button" id="copyButton" class="btn btn-warning mx-4">Copy -&gt;</button>
                            <button type="button" id="beautifyButton" class="btn btn-success">Beautify</button>
                        </div>

                    </form>

                </div>
                <div class="col">

                    <h2>Output</h2>

                    <form>

                        <div class="form-group">
                            <label for="filename">Filename</label>
                            <input type="text" id="filename" class="form-control" />
                        </div>

                        <div class="alert alert-success" id="saveAlert" role="alert" style="display: none; position: absolute; top: 2px; right: 2px">Saved!</div>

                        <div class="form-group">
                            <textarea id="output" class="form-control" rows="33" style="font-family: Consolas" spellcheck="false"></textarea>
                        </div>
                        <div class="form-group">
                            <button type="button" id="saveButton" class="btn btn-primary">Save File</button>
                            <button type="button" id="markButton" class="btn btn-success mx-4">Mark Done</button>
                            <button type="button" id="markNextButton" class="btn btn-warning mx-4">Mark &amp; Next</button>
                            <button type="button" id="nextButton" class="btn btn-danger">Next File</button>
                        </div>

                    </form>

                </div>
            </div>
        </div>

        <!-- Parameters Modal -->
        <div class="modal fade" id="paramsModal" tabindex="-1" role="dialog">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">

                        <h5 class="modal-title" id="paramsModalLabel">Set Parameter Types</h5>

                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">&times;</span>
                        </button>

                    </div>

                    <div class="modal-body">
                        
                        <div class="mb-4 ml-4">

                            <button id="setAllNumber" class="badge badge-warning mr-4">number</button>
                            <button id="setAllFloat" class="badge badge-warning mr-4">float</button>
                            <button id="setAllString" class="badge badge-warning mr-4">string</button>
                            <button id="setAllArray" class="badge badge-warning mr-4">array</button>
                            <button id="setAllGO" class="badge badge-warning mr-4">game object</button>

                        </div>

                        <div id="paramsContainer"></div>

                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="generateButton">Generate Docs</button>
                    </div>
                </div>
            </div>
        </div>

        <script type="text/javascript">

        //  Set-up the handlers

        var nextURL = '';

        $(document).ready(function () {

            $('#swapType').attr('href', 'class.html' + location.search);

            $('#nextButton').hide();
            $('#markNextButton').hide();

            var filename = getQueryString('f');

            $('#filename').val(filename);

            $('#input').load('/phaser/src/' + filename, function () {

                //  Success loading the source

                var namespace = filename.split('\\');

                namespace.pop();

                namespace.forEach(function (value, index, array) {

                    array[index] = value.charAt(0).toUpperCase() + value.substr(1);

                });

                $('#class').val('Phaser.' + namespace.join('.'));

                //  Set-up the buttons

                $('#parseButton').click(function() {

                    parseSource();

                });

                $('#beautifyButton').click(function() {

                    var src = $('#input').val();

                    var options = {
                        "indent_size": 4,
                        "indent_char": " ",
                        "indent_with_tabs": false,
                        "eol": "\n",
                        "end_with_newline": true,
                        "indent_level": 0,
                        "preserve_newlines": true,
                        "max_preserve_newlines": 10,
                        "space_in_paren": false,
                        "space_in_empty_paren": false,
                        "jslint_happy": false,
                        "space_after_anon_function": true,
                        "brace_style": "expand",
                        "unindent_chained_methods": false,
                        "break_chained_methods": false,
                        "keep_array_indentation": true,
                        "unescape_strings": false,
                        "wrap_line_length": 0,
                        "e4x": false,
                        "comma_first": false,
                        "operator_position": "before-newline"
                    };

                    $('#input').val(js_beautify(src, options));

                });

                $('#saveButton').click(function() {

                    var data = {
                        filename: $('#filename').val(),
                        src: $('#output').val()
                    };

                    //  Save to SQL
                    $.post('save.php', data, function (result) {

                        if (result == 'OK')
                        {
                            $('#saveAlert').show();
                        }

                    });

                });

                $('#copyButton').click(function() {

                    $('#output').val($('#input').val());

                });

                $('#markButton').click(function() {

                    var data = {
                        filename: $('#filename').val()
                    };

                    //  Save to SQL
                    $.post('markdone.php', data, function (result) {

                        if (result == 'OK')
                        {
                            $('#saveAlert').show();
                        }

                    });

                });

                $('#markNextButton').click(function() {

                    var data = {
                        filename: $('#filename').val()
                    };

                    //  Save to SQL
                    $.post('markdone.php', data, function (result) {

                        if (result == 'OK')
                        {
                            window.location.href = nextURL;
                        }

                    });

                });

                var data = {
                    filename: $('#filename').val()
                };

                $.post('getnext.php', data, function (result) {

                    if (result !== '')
                    {
                        console.log('Next file:', result);
                        
                        nextURL = 'function.html?f=' + result;

                        $('#nextButton').show();
                        $('#markNextButton').show();

                        $('#nextButton').click(function() {

                            window.location.href = 'function.html?f=' + result;

                        });
                    }

                });

            });

        });

        </script>

    </body>
</html>